package com.fendaren.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JSONUtil<T> {

    public  String toJSON(Object obj){
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return json;
    }

    public  T fromJson(Object obj,Class<?> clazz){
        ObjectMapper mapper = new ObjectMapper();
        T t = (T) mapper.convertValue(obj,clazz);
        return t;
    }
}
